<template>
  <el-dialog
    :title="!dataForm.archId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="患者年龄" prop="patientAge">
      <el-input v-model="dataForm.patientAge" placeholder="患者年龄"></el-input>
    </el-form-item>
    <el-form-item label="性别 0 男 1 女" prop="patientSex">
      <el-input v-model="dataForm.patientSex" placeholder="性别 0 男 1 女"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="patientBirthday">
      <el-input v-model="dataForm.patientBirthday" placeholder="出生日期"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="patientTel">
      <el-input v-model="dataForm.patientTel" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="身份证" prop="patientCard">
      <el-input v-model="dataForm.patientCard" placeholder="身份证"></el-input>
    </el-form-item>
    <el-form-item label="身高" prop="patientHight">
      <el-input v-model="dataForm.patientHight" placeholder="身高"></el-input>
    </el-form-item>
    <el-form-item label="体重 kg" prop="patientWeight">
      <el-input v-model="dataForm.patientWeight" placeholder="体重 kg"></el-input>
    </el-form-item>
    <el-form-item label="肾功能 0正常 1异常" prop="renalStatus">
      <el-input v-model="dataForm.renalStatus" placeholder="肾功能 0正常 1异常"></el-input>
    </el-form-item>
    <el-form-item label="婚姻状况 0 未婚 1已婚 2离异" prop="marryStatus">
      <el-input v-model="dataForm.marryStatus" placeholder="婚姻状况 0 未婚 1已婚 2离异"></el-input>
    </el-form-item>
    <el-form-item label="生育状况 0未生育 1备生育 2怀孕中 3已生育" prop="fertStatus">
      <el-input v-model="dataForm.fertStatus" placeholder="生育状况 0未生育 1备生育 2怀孕中 3已生育"></el-input>
    </el-form-item>
    <el-form-item label="饮酒习惯 0无饮酒 1少量 2嗜酒" prop="drinkStatus">
      <el-input v-model="dataForm.drinkStatus" placeholder="饮酒习惯 0无饮酒 1少量 2嗜酒"></el-input>
    </el-form-item>
    <el-form-item label="肝功能 0正常 1异常" prop="liverStatus">
      <el-input v-model="dataForm.liverStatus" placeholder="肝功能 0正常 1异常"></el-input>
    </el-form-item>
    <el-form-item label="患者id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="患者id"></el-input>
    </el-form-item>
    <el-form-item label="疾病史id" prop="diseasesId">
      <el-input v-model="dataForm.diseasesId" placeholder="疾病史id"></el-input>
    </el-form-item>
    <el-form-item label="疾病名称" prop="diseasesName">
      <el-input v-model="dataForm.diseasesName" placeholder="疾病名称"></el-input>
    </el-form-item>
    <el-form-item label="病人状态 1、健康  2、非健康" prop="status">
      <el-input v-model="dataForm.status" placeholder="病人状态 1、健康  2、非健康"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          archId: 0,
          patientName: '',
          patientAge: '',
          patientSex: '',
          patientBirthday: '',
          patientTel: '',
          patientCard: '',
          patientHight: '',
          patientWeight: '',
          renalStatus: '',
          marryStatus: '',
          fertStatus: '',
          drinkStatus: '',
          liverStatus: '',
          patientId: '',
          diseasesId: '',
          diseasesName: '',
          status: ''
        },
        dataRule: {
          patientName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          patientAge: [
            { required: true, message: '患者年龄不能为空', trigger: 'blur' }
          ],
          patientSex: [
            { required: true, message: '性别 0 男 1 女不能为空', trigger: 'blur' }
          ],
          patientBirthday: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          patientTel: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          patientCard: [
            { required: true, message: '身份证不能为空', trigger: 'blur' }
          ],
          patientHight: [
            { required: true, message: '身高不能为空', trigger: 'blur' }
          ],
          patientWeight: [
            { required: true, message: '体重 kg不能为空', trigger: 'blur' }
          ],
          renalStatus: [
            { required: true, message: '肾功能 0正常 1异常不能为空', trigger: 'blur' }
          ],
          marryStatus: [
            { required: true, message: '婚姻状况 0 未婚 1已婚 2离异不能为空', trigger: 'blur' }
          ],
          fertStatus: [
            { required: true, message: '生育状况 0未生育 1备生育 2怀孕中 3已生育不能为空', trigger: 'blur' }
          ],
          drinkStatus: [
            { required: true, message: '饮酒习惯 0无饮酒 1少量 2嗜酒不能为空', trigger: 'blur' }
          ],
          liverStatus: [
            { required: true, message: '肝功能 0正常 1异常不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '患者id不能为空', trigger: 'blur' }
          ],
          diseasesId: [
            { required: true, message: '疾病史id不能为空', trigger: 'blur' }
          ],
          diseasesName: [
            { required: true, message: '疾病名称不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '病人状态 1、健康  2、非健康不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.archId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.archId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/archives/info/${this.dataForm.archId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientName = data.archives.patientName
                this.dataForm.patientAge = data.archives.patientAge
                this.dataForm.patientSex = data.archives.patientSex
                this.dataForm.patientBirthday = data.archives.patientBirthday
                this.dataForm.patientTel = data.archives.patientTel
                this.dataForm.patientCard = data.archives.patientCard
                this.dataForm.patientHight = data.archives.patientHight
                this.dataForm.patientWeight = data.archives.patientWeight
                this.dataForm.renalStatus = data.archives.renalStatus
                this.dataForm.marryStatus = data.archives.marryStatus
                this.dataForm.fertStatus = data.archives.fertStatus
                this.dataForm.drinkStatus = data.archives.drinkStatus
                this.dataForm.liverStatus = data.archives.liverStatus
                this.dataForm.patientId = data.archives.patientId
                this.dataForm.diseasesId = data.archives.diseasesId
                this.dataForm.diseasesName = data.archives.diseasesName
                this.dataForm.status = data.archives.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/archives/${!this.dataForm.archId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'archId': this.dataForm.archId || undefined,
                'patientName': this.dataForm.patientName,
                'patientAge': this.dataForm.patientAge,
                'patientSex': this.dataForm.patientSex,
                'patientBirthday': this.dataForm.patientBirthday,
                'patientTel': this.dataForm.patientTel,
                'patientCard': this.dataForm.patientCard,
                'patientHight': this.dataForm.patientHight,
                'patientWeight': this.dataForm.patientWeight,
                'renalStatus': this.dataForm.renalStatus,
                'marryStatus': this.dataForm.marryStatus,
                'fertStatus': this.dataForm.fertStatus,
                'drinkStatus': this.dataForm.drinkStatus,
                'liverStatus': this.dataForm.liverStatus,
                'patientId': this.dataForm.patientId,
                'diseasesId': this.dataForm.diseasesId,
                'diseasesName': this.dataForm.diseasesName,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
